package com.lbd.gulimall.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbd.common.constrant.GiteeConstrant;
import com.lbd.common.exception.BizCodeEnum;
import com.lbd.common.to.GiteeSocialUser;
import com.lbd.common.utils.HttpUtils;
import com.lbd.gulimall.member.exception.PhoneExistException;
import com.lbd.gulimall.member.exception.UsernameExistException;
import com.lbd.gulimall.member.vo.UserLoginVO;
import com.lbd.gulimall.member.vo.UserRegisterVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lbd.gulimall.member.entity.MemberEntity;
import com.lbd.gulimall.member.service.MemberService;
import com.lbd.common.utils.PageUtils;
import com.lbd.common.utils.R;

import javax.swing.text.html.parser.Entity;


/**
 * 会员
 *
 * @author linbd
 * @email 2236000738@qq.com
 * @date 2021-06-04 17:38:22
 */
@RestController
@RequestMapping("member/member")
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;

    @PostMapping("/socialUser")
    public R socialLogin(@RequestBody GiteeSocialUser socialUser) {
        //根据access_token获取用户资源信息  https://gitee.com/api/v5/user
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("access_token",socialUser.getAccessToken());
        try {
            HttpResponse httpResponse = HttpUtils.doGet(GiteeConstrant.address, GiteeConstrant.userInfoPath, "get", new HashMap<>(), paramMap);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String json = EntityUtils.toString(httpResponse.getEntity());
                MemberEntity entity = memberService.socialLogin(json,socialUser);
                R r = R.ok();
                r.setData(entity);
                return r;
            }else {
                return R.error(BizCodeEnum.HTTP_CALL_EXCEPTION.getCode(),BizCodeEnum.HTTP_CALL_EXCEPTION.getMsg());
            }
        } catch (Exception e) {
            return R.error(BizCodeEnum.HTTP_CALL_EXCEPTION.getCode(),BizCodeEnum.HTTP_CALL_EXCEPTION.getMsg());
        }
    }

    @PostMapping("/login")
    public R login(@RequestBody UserLoginVO userLoginVO) {
        MemberEntity memberEntity = memberService.login(userLoginVO);
        if (memberEntity == null) {
            return R.error(BizCodeEnum.USERNAME_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.USERNAME_PASSWORD_INVALID_EXCEPTION.getMsg());
        } else {
            R r = R.ok();
            r.setData(memberEntity);
            return r;
        }
    }

    @PostMapping("/register")
    public R register(@RequestBody UserRegisterVO userRegisterVO) {
        try {
            memberService.register(userRegisterVO);
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnum.USERNAME_REPEAT_EXCEPTION.getCode(), BizCodeEnum.USERNAME_REPEAT_EXCEPTION.getMsg());
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_REPEAT_EXCEPTION.getCode(), BizCodeEnum.PHONE_REPEAT_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @RequestMapping("/test")
    public R test() {
        MemberEntity entity = memberService.getOne(new QueryWrapper<MemberEntity>().eq("id", 1));
        log.info("9000用户微服务，返回值：{}", entity);
        return R.ok().put("memberInfo", entity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
